from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Model
from django.urls import reverse
from django.utils.translation import gettext_lazy as _


class User(AbstractUser):
    class Meta:
        db_table = "django_user"
        verbose_name = "用户"
        verbose_name_plural = "用户"

    name = models.CharField(_("Name of User"), blank=True, max_length=255)
    first_name = None
    last_name = None
    staffcode = models.CharField(max_length=10, unique=True, verbose_name="员工编码")
    cnName = models.CharField(max_length=10, verbose_name="中文名称")

    def get_absolute_url(self):
        return reverse("users:detail", kwargs={"username": self.username})


class Role(Model):
    class Meta:
        db_table = "role"
        verbose_name = "角色"
        verbose_name_plural = "角色"

    name = models.CharField(max_length=20, unique=True, verbose_name="名称")
    chinese_name = models.CharField(max_length=20, blank=True, verbose_name="中文名称")

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.chinese_name if self.chinese_name else self.name


class Flow(Model):
    class Meta:
        db_table = "flow"
        verbose_name = "流程"
        verbose_name_plural = "流程"

    name = models.CharField(
        max_length=20, unique=True, null=True, blank=True, verbose_name="名称"
    )
    chinese_name = models.CharField(max_length=20, null=True, editable=True, verbose_name="中文名称")
    leader_check = models.BooleanField(verbose_name="领导检查")
    stone_exam = models.BooleanField(verbose_name="验石")
    scan_and_record = models.BooleanField(default=True, verbose_name="扫描并记录")
    qc_check = models.BooleanField(default=True, verbose_name="品质检查")

    dispatcher_check = models.BooleanField(default=True, verbose_name="中央收貨")


    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.chinese_name if self.chinese_name else self.name


class Site(Model):
    class Meta:
        db_table = "site"
        verbose_name = "购货单位"
        verbose_name_plural = "购货单位"

    name = models.CharField(
        max_length=5, primary_key=True, editable=True, verbose_name="名称"
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class Department(Model):
    class Meta:
        db_table = "department"
        verbose_name = "部门"
        verbose_name_plural = "部门"

    code = models.CharField(
        max_length=10, primary_key=True, editable=True, verbose_name="部门编码"
    )  # department code
    name = models.CharField(max_length=10, verbose_name="部门名称")  # department name
    # site = models.CharField(max_length=5)
    site = models.ForeignKey(Site, db_column="site", on_delete=models.PROTECT, verbose_name="购货单位")  # source
    flow = models.ForeignKey(
        Flow, db_column="flow", on_delete=models.PROTECT, verbose_name="工艺节点"
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.code


class Printer(Model):
    class Meta:
        db_table = "printer"
        verbose_name = "打印机"
        verbose_name_plural = "打印机"

    description = models.CharField(max_length=30, null=True, verbose_name="描述")
    department = models.IntegerField(choices=[(1, "金仓"), (2, "石仓")], verbose_name="仓")
    group = models.CharField(max_length=30, blank=True, verbose_name="组别")
    # site = models.ForeignKey(
    #     Site, db_column="site", on_delete=models.PROTECT, null=True, verbose_name="购货单位"
    # )
    site = models.CharField(max_length=5, choices=[("4083", "內销"), ("1062", "外销")], verbose_name="內/外销")

    printId = models.PositiveSmallIntegerField(
        blank=True, verbose_name="打印ID"
    )

    default = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.description


class Account(Model):
    class Meta:
        db_table = "account"

    # id = models.CharField(max_length=10, primary_key=True)
    user_code = models.CharField(max_length=10)
    django_user = models.ForeignKey(User, on_delete=models.PROTECT)
    # role = models.ManyToManyField(Role)
    role = models.ForeignKey(Role, on_delete=models.PROTECT)
    department = models.ForeignKey(
        Department, db_column="department_code", on_delete=models.PROTECT
    )
    default_printer = models.ForeignKey(
        Printer, db_column="default_printer", on_delete=models.PROTECT
    )
    enable = models.BooleanField(default=True)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.user_code


class RolePermission(Model):
    class Meta:
        db_table = "role Permission"
        verbose_name = "角色权限"
        verbose_name_plural = "角色权限"
        unique_together=["role", "flow"]

    role = models.ForeignKey(Role, on_delete=models.PROTECT, verbose_name="角色")
    flow = models.ForeignKey(
        Flow, db_column="flow", on_delete=models.PROTECT, verbose_name="工艺节点"
    )
    show_Order_Number = models.BooleanField(default=True, verbose_name="展示发订单编号")
    show_Due_Date = models.BooleanField(default=True, verbose_name="展示交货日期")
    show_Quantity = models.BooleanField(default=True, verbose_name="展示件数")
    show_Cargo_type = models.BooleanField(default=True, verbose_name="展示货型")
    show_Description = models.BooleanField(default=True, verbose_name="展示描述")
    show_Product_Size = models.BooleanField(default=True, verbose_name="展示产品大小")
    show_Fineness = models.BooleanField(default=True, verbose_name="展示成色")
    show_Purchases_Unit = models.BooleanField(default=True, verbose_name="展示购货单位")
    show_Stone_Date = models.BooleanField(default=True, verbose_name="展示上石日期")
    show_Flower_Patten = models.BooleanField(default=True, verbose_name="展示花头号数")
    show_Manifest_Type = models.BooleanField(default=True, verbose_name="展示货单类别")
    show_Design_Number = models.BooleanField(default=True, verbose_name="展示设计编号")
    show_Model_Number = models.BooleanField(default=True, verbose_name="展示模号")
    show_Black_And_White_Photo = models.BooleanField(
        default=True, verbose_name="展示黑白图"
    )
    show_Production_Instructions = models.BooleanField(
        default=True, verbose_name="展示生产指示"
    )
    show_Result = models.BooleanField(default=True, verbose_name="展示效果")
    show_Company_Engraving_Content = models.BooleanField(
        default=True, verbose_name="展示公司字印(内容)"
    )
    show_Company_Engraving_Font = models.BooleanField(
        default=True, verbose_name="展示公司字印(字体)"
    )
    show_Customer_Engraving_Content = models.BooleanField(
        default=True, verbose_name="展示客人字印(内容)"
    )
    show_Customer_Engraving_Font = models.BooleanField(
        default=True, verbose_name="展示客人字印(字体)"
    )

    show_Engraving_Description = models.BooleanField(
        default=True, verbose_name="展示字印(客印说明)"
    )
    show_Engraving_Effect = models.BooleanField(
        default=True, verbose_name="展示字印(客印效果)"
    )
    show_Ingredients = models.BooleanField(default=True, verbose_name="展示原料清单")
    show_Stone_Issue = models.BooleanField(default=True, verbose_name="展示石问题")
    show_Stone_Detail = models.BooleanField(default=True, verbose_name="展示发石明细资料")
    show_Production_Notes = models.BooleanField(default=True, verbose_name="展示生产备注")
    show_Process = models.BooleanField(default=True, verbose_name="展示工艺路线")
    show_Difficulty = models.BooleanField(default=True, verbose_name="展示难易程度")
    show_Divided_member = models.BooleanField(default=True, verbose_name="展示分件数量")
    show_Production_weight = models.BooleanField(default=True, verbose_name="展示生产实重")
    show_Registration_From = models.BooleanField(default=True, verbose_name="展示工序登记表")
    show_Row_Stone_Image = models.BooleanField(default=True, verbose_name="展示排石图")
    show_Colour_Image = models.BooleanField(default=True, verbose_name="展示彩图")


    menu_massscan_perm = models.BooleanField(default=True, verbose_name="批次扫描权限", help_text="工人/QC/组长/验石员才可开设权限")
    menu_orderview_perm = models.BooleanField(default=True, verbose_name="工单查询")
    menu_printlabel_perm = models.BooleanField(default=True, verbose_name="打印标签权限")

    report_worker_storage_perm  = models.BooleanField(default=True, verbose_name="查看员工货存查询/存金报告")
    report_worker_transaction_perm = models.BooleanField(default=True, verbose_name="查看员工交收报表二 (手工帐)")
    report_worker_handset_perm = models.BooleanField(default=True, verbose_name="查看员工交收报表")
    report_worker_handover_perm = models.BooleanField(default=True, verbose_name="查看员工签收报表")

    report_qc_handset_perm = models.BooleanField(default=True, verbose_name="查看QC实时存货报表")
    report_qc_transaction_perm = models.BooleanField(default=True, verbose_name="查看QC检测货品交易报表")
    report_printstatus_perm = models.BooleanField(default=True, verbose_name="查看打印报表")
    report_leadtime_perm = models.BooleanField(default=True, verbose_name="查看LEADTIME报表")
    
    menu_updatert_perm = models.BooleanField(default=True, verbose_name="上传工单流程图")

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
       return f"{self.flow} - {self.role}"
